/**
 * Created by CJW on 2017/3/7.
 */
const APPERROR = require('../rest').APIError;
let gid = 0;
const nextId =()=>{

    gid++;
    return 't'+gid;
}

var todos = [
    {
        id: nextId(),
        name: 'Learn Git',
        description: 'Learn how to use git as distributed version control'
    },
    {
        id: nextId(),
        name: 'Learn JavaScript',
        description: 'Learn JavaScript, Node.js, NPM and other libraries'
    },
    {
        id: nextId(),
        name: 'Learn Python',
        description: 'Learn Python, WSGI, asyncio and NumPy'
    },
    {
        id: nextId(),
        name: 'Learn Java',
        description: 'Learn Java, Servlet, Maven and Spring'
    }
];

module.exports ={
    /**
     * 获取所有的数据
     * @param ctx
     * @param next
     * @returns {Promise.<void>}
     */
    'GET /api/todos':async (ctx,next)=>{
        ctx.rest({
            todos:todos
        });
    },
    /**
     * 添加数据
     * @param ctx
     * @param next
     * @returns {Promise.<module.exports.APIError>}
     * @constructor
     */
    'POST /api/todos':async (ctx,next)=>{
        let t = ctx.request.body;
        let todo;
        if(!t.name || !t.name.trim()){
            return new APPERROR('invalid_input', 'Missing name');
        }
        if(!t.description || !t.description){
            return new APPERROR('invalid_input', 'Missing description');
        }
        todo = {
            id:nextId(),
            name:t.name.trim(),
            description:t.description.trim()
        }
        todos.push(todo);
        ctx.rest(todo)
    },
    /**
     * 删除数据
     * @param ctx
     * @param next
     * @returns {Promise.<void>}
     */
    'DELETE /api/todos/:id':async (ctx,next)=> {
        let index;
        for (var i = 0; i < todos.length; i++) {
            if (todos[i].id = ctx.params.id) {
                index = i;
                break;
            }
        }
        if (index === -1) {
            throw new APIError('notfound', 'Todo not found by id: ' + ctx.params.id);
        }
        //返回被删除的数据
        ctx.rest(todos.splice(index, 1)[0]);
    },
    /**
     * 修改数据
     * @param ctx
     * @param next
     * @returns {Promise.<module.exports.APIError>}
     * @constructor
     */
    'PUT /api/todos/:id':async (ctx,next)=>{
        let t = ctx.request.body;
        let index = -1;
        let i,todo;
        if(!t.name || !t.name.trim()){
            return new APPERROR('invalid_input', 'Missing name');
        }
        if(!t.description || !t.description){
            return new APPERROR('invalid_input', 'Missing description');
        }
        for (var j = 0;j < todos.length; j++) {
            if (todos[j].id = ctx.params.id) {
                index = j;
                break;
            }
        }
        if (index === -1) {
            throw new APIError('notfound', 'Todo not found by id: ' + ctx.params.id);
        }
        todo = todos[index];
        todo.name = t.name.trim();
        todo.description = t.description.trim();
        ctx.rest(todo);

    }


}